package SQL;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import usuaris.Empresa;

import anuncios.Oferta;
//import usuaris.Alumne;
public class Actividad {
	
	Conect conector = new Conect();
	Connection con= conector.conect(); 
	
	List <Oferta> ofertas = new ArrayList <Oferta>();
	
	private String id_inscribe; // cif o dni, cadena
	//private Integer id_oferta;
	
	public String getId_inscribe() {
		return id_inscribe;
	}
	public void setId_inscribe(String id_inscrib) {
		this.id_inscribe = id_inscrib;
	}
	
	public String borrarOferta(String id){
		
		
		String respuesta="-";
		
		try{
			Statement st = con.createStatement();
			st.executeUpdate("DELETE from oferta where id="+id);
			respuesta="Borrado correctamente";
		}catch(Exception e){
			respuesta= "Error borrando oferta";
		}finally{
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return respuesta;
	}
	
	public Integer mostrarIns() throws SQLException{
		Statement stmt = con.createStatement();
		String sql = "SELECT id_inscribe from inscripcion where id_inscribe="+id_inscribe;                    
		ResultSet rs2= stmt.executeQuery(sql);
				
		return (Integer) rs2.getObject("id_oferta");	
	}
	
	public Integer mostrarCre() throws SQLException{ //
		Statement stmt = con.createStatement();
		String sql = "SELECT id_inscribe from inscripcion where id_inscribe="+id_inscribe;                    
		ResultSet rs2= stmt.executeQuery(sql);		
		return (Integer) rs2.getObject("id_oferta");	
	}
	
	public void mostrarInscritas(String id_ins){
		Statement st;
		ResultSet rs1;
		try {
			st = con.createStatement();
			rs1 = st.executeQuery("select * from oferta where id in (select id_oferta from inscripcion where id_inscribe='"+ id_ins + "')");
			while (rs1.next()) {
				ofertas.add( new Oferta( rs1.getInt("id"),rs1.getInt("activa"), rs1.getInt("em_alum"),
						rs1.getString("titol"), rs1.getString("descripcio"), rs1.getString("identificador"), rs1.getString("tipus_cicle")));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
		
		public void mostrarPublicadas(String id_ins){
			Statement st;
			ResultSet rs1;
			try {
				st = con.createStatement();
				rs1 = st.executeQuery("select * from oferta where identificador='"	+ id_ins + "'");
				while (rs1.next()) {
					ofertas.add( new Oferta( rs1.getInt("id"),rs1.getInt("activa"), rs1.getInt("em_alum"),
							rs1.getString("titol"), rs1.getString("descripcio"), rs1.getString("identificador"), rs1.getString("tipus_cicle")));
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
				try {
					con.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
	}
	public List<Oferta> getOfertas() {
		return ofertas;
	}
	
	
}
